

var TextForm = function( action, vars, modid )	{
	
	
	Object.extend( obj = xhttp.checkscope( '.TextForm' ), TextForm.proto );
	Object.extend( obj, TextForm[ "proto_" + action ]);
	
	obj.modid		= modid;
	obj.form		= obj.down( "form" );
	obj.form.action	= "/xhttp.mod";
	
	return obj.init( vars );
	
}

// GENERAL PROTOS
TextForm.proto = {
	
	foo			: function() {
	
		return "Bar.";
	},
	
	init		: function( vars ) {
			
		this.vars = vars;
				
		if( vars.validate )
			for ( id in vars.validate ) 
				this.down( '#'+ id ).observe( vars.validate[ id ].event, this[ "validate_" + vars.validate[ id ].action ].bind( this )).vars = vars.validate[ id ];
		
		if( vars.actions )
			for ( id in vars.actions ) 
				this.down( '[name="'+ id +'"]' ).observe( "click", this[ vars.actions[ id ]].bind( this ));
			
		
		return this;
	},
	
	push			: function() {

		this.form.request( {
			parameters: { set: Object.toJSON({ mod: "text.form", action:"push" })},
			evalJSON: 'force',
			onComplete: this.confirmed.bind(this)
		} );
	},
	
	submit			: function( e ) {
		
		el = e.element();	
		el.value = "connecting...";
		
		if( el.vars && typeof el.vars.succ == "string" ) {

			var succ	= el.vars.succ;
			var fb	= '';
			var tw	= '';
			
			if( el.vars.social ) {
				if( el.vars.social.twitter ) {
					tw = " <a class='social twitter' title='post to twitter' href='http://twitter.com/home/?status=" + el.vars.social_message + "' target='_blank'>Post to twitter</a>";
				}
				
				if ( el.vars.social.facebook ) {
					fb = " <a class='social facebook' title='post to facebook' href='http://facebook.com/share.php?u=" + el.vars.social_message + "' target='_blank'>Post to facebook</a>";
				}
			}
			
			if( el.vars.growl ) {
				var growl 	= new Growl({'content': el.vars.growl.success, 'duration': 1, 'delay': 5 }); 
			}
			
			succ = succ + '<br />' + tw +fb;
			el.up(0).update( succ );
		}

		if( $('cancel') != undefined ) {
			$('cancel').hide();
		}
		
		this.form.request({
			parameters: { set: Object.toJSON({ mod: "text.form", action:"query", id:this.modid })},
			evalJSON: 'force',
			onComplete: this.confirmed.bind( this )
		});
			
		this.form.disable();
	},
	
	confirmed	: function( response ) {
		
		var set = response.responseJSON;

		if( set.update ) {
			this.form.update( set.update );
		}
		
		if( set.func ) {
			this[ set.func ]();
		}

	},
	
	redirect	: function() {
		
		if( this.vars.redirect == 'self' )
			window.location = window.location.href;
		else
			window.location = this.vars.redirect;
	}

}

// ACTION SPECIFIC OVERWRITES
TextForm.proto_sequence = {

	init			: function( validate ) {
	
		for ( val in validate ) 
			$( val ).observe( validate[ val ].event, this[ "validate_" + validate[ val ].action ].bind( this,  validate[ val ], val )).vars = validate[ val ];
		
		//xhttp.loadModule( "text.note" );
		
		return this;
	},
	
	validate_connect: function( act, el ) {
	
		$( el ).up(2).hide();
		this.select( '.' + act.connect )[0].removeClassName('hide');
	},
	
	validate_redirect	: function( act, el ) {
		
		window.location = act.redirect;

	},
	
	validate_req	: function( elem, act, e ) {
	
		el = e.element();
		el.pass = true;
		
		this.form.getElements().each( function( node ) {
	
			if( $( node.parentNode ).hasClassName("req") && (!node.getValue() || ( el.vars && $( node.parentNode ).hasClassName( "err" )))) {
				
				el.pass = false;
				$( node.parentNode ).addClassName( "err" );
			} else
				$( node.parentNode ).removeClassName( "err" );
			
		});
		
		if( el.pass ) this.submit( e );
		else this.note( "stick", ( el.vars.err )? el.vars.err : "Error.", el );
	}
	
}


TextForm.proto_simple = {
	
		
	login			: function( e ) {
		
		this.form.request({
			parameters: { set: Object.toJSON({ "mod": "text.form", "action": "login" })},
			evalJSON: 'force',
			onComplete: this.confirmed.bind(this)
		});
		
	},
	
	
	logout 			: function ( e ) {
	
		this.form.request({
			parameters: { set: Object.toJSON({ "mod": "text.form", "action": "logout" })},
			evalJSON: 'force',
			onComplete: this.confirmed.bind(this)
		});
	}, 
	
		
	connect			: function ( e ) {
	
		xhttp.construct( Number( this.vars.connect ), this.wrap(), 1 );
				
	},
	
	forgot_pass		: function ( e ) {
	

		this.form.request({
			parameters: { set: Object.toJSON({ "mod": "text.form", "action": "forgot_pass","modid": this.vars.sendin.modid })},
			evalJSON: 'force',
			onComplete: this.confirmed.bind(this)
		});
	
	}
	
	

}


TextForm.proto_label = {

	init			: function( validate ) {
		
		for ( val in validate ) 
			$( val ).observe( validate[ val ].event, this[ "validate_" + validate[ val ].action ].bind( this )).vars = validate[ val ];
		
		//xhttp.loadModule( "text.note" );
		
		return this;
	},
	
	note			: function( type, cont, target ) {
		
		if( !cont && this[ "note_"+ type ] ){
			this[ "note_"+ type ].remove()
			return this[ "note_"+ type ] = false;
		}
		
		if( !this[ "note_"+ type ] )
			if( type == "stick" ) this.insert( this[ "note_"+ type ] = new TextNote( type, { target:target }, true ));
			else target.up(0).insert( this[ "note_"+ type ] = new TextNote( type, { target:target }, true ));
		
		this[ "note_"+ type ].set( cont, target );
	},
	
	validate_reg	: function( e ) {

		el = e.element();
		regexp = new RegExp( el.vars.cond );
		
		if( !regexp.test( el.getValue() )) {
			
			$( el.parentNode ).addClassName( "err" );
			this.note( "stick", ( el.vars.err )? el.vars.err : "Error.", el );
			
		} else {
			this.note( "stick", false );
			$( el.parentNode ).removeClassName( "err" );
		}
	},
	
	validate_print	: function( e ) {
	
		window.print();
		
	},
	
	validate_clone	: function( e ) {
	
		el = e.element();

		if( $( el.vars.cond ).getValue() != el.getValue() || !el.getValue() ) {
			$( el.parentNode ).addClassName( "err" );
			this.note( "stick", ( el.vars.err )? el.vars.err : "Error.", el );
			
		} else {
			$( el.parentNode ).removeClassName( "err" );
			this.note( "stick", false );
		}
	},
	
	validate_calc	: function( e ) {
	
		el = e.element();
		var amount	= eval( Number( el.value ) + el.vars.cond + Number( el.vars.val ));
		
		$( el.vars.holder ).value = amount;
	
	},
	
	validate_cancel	: function( e ) {
	
		this.previous(0).remove();
		this.remove();
	},
	
	validate_req	: function( e ) {
		
		el = e.element();
		el.pass = true;
		
		this.form.getElements().each( function( node ) {
	
			if( $( node.parentNode ).hasClassName("req") && (!node.getValue() || ( el.vars && $( node.parentNode ).hasClassName( "err" )))) {
				
				el.pass = false;
				$( node.parentNode ).addClassName( "err" );
			} else
				$( node.parentNode ).removeClassName( "err" );
			
		});
		
		if( el.pass ) this.submit( e );
		else this.note( "stick", ( el.vars.err )? el.vars.err : "Error.", el );
	}

}



// add textnote to form (should be seperated from this module)!!
var TextNote = function( action, vars, blank )	{
	
	Object.extend(
		obj = (blank)? new Element( "div", { "class":"TextNote " + action }) : $$( '.TextNote' ).pop(), 
		TextNote[ "proto_" + action ]
	);
	
	obj.vars = vars;
	return obj.init( );	

}



// ACTION SPECIFIC PROTO'S
TextNote.proto_stick = {
	init		: function() {
		
		return this;
	},
	
	set			: function( string, trg ) {
		
		this.update( string );
		if( trg ) this.clonePosition( trg, { setWidth:false, setHeight:false, offsetTop: trg.getHeight() + 2 });
	}

}